package com.le.tester.imooc.masterJUC.lock;

import java.util.Random;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

/**
 * 描述：使用tryLock来避免死锁
 */
public class TryLockDeadLock implements Runnable {

    private int flag = 1;
    private static Lock lock1 = new ReentrantLock();
    private static Lock lock2 = new ReentrantLock();

    public static void main(String[] args) {
        TryLockDeadLock tryLockDeadLock1 = new TryLockDeadLock();
        TryLockDeadLock tryLockDeadLock2 = new TryLockDeadLock();
        tryLockDeadLock1.flag = 1;
        tryLockDeadLock2.flag = 0;
        new Thread(tryLockDeadLock1).start();
        new Thread(tryLockDeadLock2).start();


    }

    @Override
    public void run() {
        //尝试的获取两把锁
        for (int i = 0; i < 100; i++) {
            if (flag == 1) {
                try {
                    //tryLock本身会返回一个boolean,尝试的去获取锁，采用if的形式来获取这个锁
                    if (lock1.tryLock(800, TimeUnit.MILLISECONDS)) {

                        try {
                            System.out.println("线程1获取到了锁1");
                            Thread.sleep(500);

                            if (lock2.tryLock(800, TimeUnit.MILLISECONDS)) {
                                try {
                                    System.out.println("线程1获取到了锁2");
                                    System.out.println("线程1获取到了两把锁");
                                    break;
                                } finally {
                                    lock2.unlock();
                                }
                            }
                            //
                        } finally {
                            lock1.unlock();
                            //解锁后休眠，让别人去竞争这个锁
                            Thread.sleep(new Random().nextInt(1000));
                        }
                    } else {
                        System.out.println("线程1获取锁1失败，已重试！");
                    }
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }

            if (flag == 0) {
                try {
                    //tryLock本身会返回一个boolean,尝试的去获取锁，也那怪减一采用if的形式来获取这个锁
                    if (lock2.tryLock(1500, TimeUnit.MILLISECONDS)) {

                        try {
                            System.out.println("线程2获取到了锁2");
                            Thread.sleep(500);

                            if (lock1.tryLock(1500, TimeUnit.MILLISECONDS)) {
                                try {
                                    System.out.println("线程2获取到了锁1");
                                    System.out.println("线程2获取到了两把锁");
                                    break;
                                } finally {
                                    lock1.unlock();
                                }
                            }
                            //
                        } finally {
                            lock2.unlock();
                            //解锁后休眠，让别人去竞争这个锁
                            Thread.sleep(new Random().nextInt(1000));
                        }
                    } else {
                        System.out.println("线程2获取锁2失败，已重试！");
                    }
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}
